package test3.main;

import test3.controller.PageController;
import test3.controller.impl.PageControllerImpl;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Index extends JFrame {
	private PageController controller=new PageControllerImpl();

	CardLayout card ;

	JMenuBar menubar;
	JMenu add;
	JMenu delete;
	JMenu query;
	JMenu modify;
	JMenu userDocument;
	JMenu copyright;
	JMenuItem addEmployee;
	JMenuItem addAdministrator;
	JMenuItem deleteEmployee;
	JMenuItem queryEmployee;
	JMenuItem queryAllEmployee;
	JMenuItem modifyEmployee;
	JMenuItem userDocumentOne;
	JMenuItem developer;
	JMenuItem copyDocument;
	

	JPanel panel;
	//JPanel loginPanel;
	JPanel addAdminPanel;
	JPanel addEmployeePanel;
	JPanel deletePanel;
	JPanel queryOnePanel;
	JScrollPane queryAllPanel;
	JTextArea queryAllText;
	JPanel modifyEmployeePanel;
	JScrollPane instructionPanel;
	JScrollPane developerPanel;
	JScrollPane copyDocumentPanel;
	JPanel welcomePanel;
	
	//增加管理员信息
	JTextField addAdminIdText;		
	JTextField addAdminPasswordText;
	
	//增加用户信息
	JTextField addEmployeeIdText;
	JTextField addEmployeeNameText;
	JTextField addEmployeeAgeText;
	JTextField addEmployeeGenderText;
	
	//查询一个用户信息
	JTextField queryOneEmployeeIdText;
	JTextField queryOneEmployeeNameText;
	JTextField queryOneEmployeeAgeText;
	JTextField queryOneEmployeeGenderText;
	
	//修改用户
	JTextField modifyEmployeeIdText;
	JTextField modifyEmployeeNameText;
	JTextField modifyEmployeeAgeText;
	JTextField modifyEmployeeGenderText;
	JTextField modifyEmployeeQueryIdText;
	
	
	public Index() throws HeadlessException {
		super();
	}
	public Index(String title,int x,int y,int width,int height) {
		card = new CardLayout();
		initMenu(title);
		initBody();
		setLocation(x,y);
		setSize(width,height);
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		add(panel);
		this.setResizable(false);


		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.out.println("触发windowClosing事件");
			}

			public void windowClosed(WindowEvent e)
			{
				System.out.println("触发windowClosed事件");

				controller.exit();
			}
		});


	}
	void initMenu(String title) {
		setTitle(title);
		menubar = new JMenuBar();
		add = new JMenu("Add");
		addAdministrator = new JMenuItem("add administrator");
		addEmployee = new JMenuItem("add employee",new ImageIcon("user.png"));
		add.add(addAdministrator);
		add.add(addEmployee);
		addAdministrator.addActionListener(new MenuAction(this));
		addEmployee.addActionListener(new MenuAction(this));
		
		delete = new JMenu("Delete");
		deleteEmployee = new JMenuItem("delete employee");
		delete.add(deleteEmployee);
		deleteEmployee.addActionListener(new MenuAction(this));
		
		query = new JMenu("Query");
		queryEmployee = new JMenuItem("query one employee");
		queryAllEmployee = new JMenuItem("query all employees"); 
		query.add(queryEmployee);
		query.add(queryAllEmployee);
		queryEmployee.addActionListener(new MenuAction(this));
		queryAllEmployee.addActionListener(new MenuAction(this));
		
		modify = new JMenu("Modify");
		modifyEmployee = new JMenuItem("modify employee");
		modify.add(modifyEmployee);
		modifyEmployee.addActionListener(new MenuAction(this));
		
		userDocument = new JMenu("Instructions");
		userDocumentOne = new JMenuItem("instruction");
		developer = new JMenuItem("developer");
		userDocument.add(userDocumentOne);
		userDocument.add(developer);
		userDocumentOne.addActionListener(new MenuAction(this));
		developer.addActionListener(new MenuAction(this));
		
		copyright = new JMenu("Copy");
		copyDocument = new JMenuItem("copy document");
		copyright.add(copyDocument);
		copyDocument.addActionListener(new MenuAction(this));
		
		menubar.add(add);
		menubar.add(delete);
		menubar.add(query);
		menubar.add(modify);
		menubar.add(userDocument);
		menubar.add(copyright);
		
		setJMenuBar(menubar);
	}
	void initBody() {
		panel = new JPanel();
		panel.setLayout(this.card);

		initWelcomePanel();
		panel.add("welcome", welcomePanel);

		initAddAdminPanel();
		panel.add("addAdmin", addAdminPanel);

		initAddEmployeePanel();
		panel.add("addEmployee", addEmployeePanel);

		initDeleteEmployeePanel();
		panel.add("deleteEmployee", deletePanel);
		

		initQueryOneEmployeePanel();
		panel.add("queryOneEmployee", queryOnePanel);
		

		initQueryAllEmployeePanel();
		panel.add("queryAllEmployee",queryAllPanel);
		
		//�޸��û���Ϣ
		initModifyEmployeePanel();
		panel.add("modifyEmployee",modifyEmployeePanel);

		initInstructionPanel();
		panel.add("instruction", instructionPanel);

		initDeveloperPanel();
		panel.add("developer", developerPanel);

		initCopyDocumentPanel();
		panel.add("copyDocument", copyDocumentPanel);
		
		
	}
	void initAddAdminPanel() {
		addAdminPanel = new JPanel();
		addAdminPanel.setLayout(null);
		JLabel title = new JLabel("Add Administrator Module");
		JLabel id = new JLabel("User ID:");
		JLabel password = new JLabel("Password:");
		addAdminIdText = new JTextField(20);
		addAdminPasswordText= new JTextField(20);
		JButton register = new JButton("Register");
		JButton cancel = new JButton("Cancel");
		addAdminPanel.add(title);
		title.setBounds(260, 80, 400, 30);
		title.setFont(new Font("����",Font.BOLD,20));
		addAdminPanel.add(id);
		id.setBounds(220,150,100,30);
		addAdminPanel.add(addAdminIdText);
		addAdminIdText.setBounds(300, 150, 250, 30);
		addAdminPanel.add(password);
		password.setBounds(220,220,100,30);
		addAdminPanel.add(addAdminPasswordText);
		addAdminPasswordText.setBounds(300, 220, 250, 30);
		addAdminPanel.add(register);
		register.setBounds(250, 300, 100, 30);
		addAdminPanel.add(cancel);
		cancel.setBounds(420, 300, 100, 30);
		
		register.addActionListener(new AddAdminAction(this));
		cancel.addActionListener(new AddAdminAction(this));
	}
	void initAddEmployeePanel() {
		addEmployeePanel = new JPanel();
		addEmployeePanel.setLayout(null);
		JLabel title = new JLabel("Add Employee Module");
		JLabel id = new JLabel("id:");
		JLabel name = new JLabel("name:");
		JLabel age = new JLabel("age:");
		JLabel gender = new JLabel("gender:");
		addEmployeeIdText= new JTextField(20);
		addEmployeeNameText = new JTextField(20);
		addEmployeeAgeText= new JTextField(20);
		addEmployeeGenderText= new JTextField(20);
		JButton submit = new JButton("submit");
		JButton reset = new JButton("reset");
		addEmployeePanel.add(title);
		title.setFont(new Font("����",Font.BOLD,24));
		title.setBounds(260, 50, 400, 30);
		addEmployeePanel.add(id);
		id.setBounds(210, 120, 50, 30);
		id.setFont(new Font("����",Font.BOLD,16));
		addEmployeePanel.add(addEmployeeIdText);
		addEmployeeIdText.setBounds(280, 120, 280, 30);
		addEmployeePanel.add(name);
		name.setBounds(210, 170, 50, 30);
		name.setFont(new Font("����",Font.BOLD,16));
		addEmployeePanel.add(addEmployeeNameText);
		addEmployeeNameText.setBounds(280, 170, 280, 30);
		addEmployeePanel.add(age);
		age.setBounds(210, 220, 50, 30);
		age.setFont(new Font("����",Font.BOLD,16));
		addEmployeePanel.add(addEmployeeAgeText);
		addEmployeeAgeText.setBounds(280, 220, 280, 30);
		addEmployeePanel.add(gender);
		gender.setBounds(210, 270, 70, 30);
		gender.setFont(new Font("����",Font.BOLD,16));
		addEmployeePanel.add(addEmployeeGenderText);
		addEmployeeGenderText.setBounds(280, 270, 280, 30);
		addEmployeePanel.add(submit);
		submit.setBounds(240, 350, 100, 30);
		addEmployeePanel.add(reset);
		reset.setBounds(420, 350, 100, 30);
		
		submit.addActionListener(new AddEmployeeAction(this));
		reset.addActionListener(new AddEmployeeAction(this));
	}
	void initQueryOneEmployeePanel() {
		queryOnePanel = new JPanel();
		queryOnePanel.setLayout(null);
		JLabel title = new JLabel("Query Employee Module");
		JLabel queryId = new JLabel("Please enter the ID:");
		JLabel id = new JLabel("id:");
		JLabel name = new JLabel("name:");
		JLabel age = new JLabel("age:");
		JLabel gender = new JLabel("gender:");
		queryOneEmployeeIdText= new JTextField(20);
		queryOneEmployeeNameText= new JTextField(20);
		queryOneEmployeeAgeText= new JTextField(20);
		queryOneEmployeeGenderText= new JTextField(20);
		JTextField queryIdText = new JTextField(20);
		JButton submit = new JButton("submit");
		queryOnePanel.add(queryId);
		queryId.setBounds(160, 120, 150, 30);
		queryOnePanel.add(queryIdText);
		queryIdText.setBounds(280, 120, 230, 30);
		queryOnePanel.add(submit);
		submit.setBounds(520, 120, 100, 30);
		queryOnePanel.add(title);
		title.setFont(new Font("����",Font.BOLD,24));
		title.setBounds(260, 50, 400, 30);
		queryOnePanel.add(id);
		id.setBounds(210, 180, 50, 30);
		id.setFont(new Font("����",Font.BOLD,16));
		queryOnePanel.add(queryOneEmployeeIdText);
		queryOneEmployeeIdText.setBounds(280, 180, 280, 30);
		queryOnePanel.add(name);
		name.setBounds(210, 230, 50, 30);
		name.setFont(new Font("����",Font.BOLD,16));
		queryOnePanel.add(queryOneEmployeeNameText);
		queryOneEmployeeNameText.setBounds(280, 230, 280, 30);
		queryOnePanel.add(age);
		age.setBounds(210, 280, 50, 30);
		age.setFont(new Font("����",Font.BOLD,16));
		queryOnePanel.add(queryOneEmployeeAgeText);
		queryOneEmployeeAgeText.setBounds(280, 280, 280, 30);
		queryOnePanel.add(gender);
		gender.setBounds(210, 330, 70, 30);
		gender.setFont(new Font("����",Font.BOLD,16));
		queryOnePanel.add(queryOneEmployeeGenderText);
		queryOneEmployeeGenderText.setBounds(280, 330, 280, 30);
		
		submit.addActionListener(new QueryOneEmployeeAction(this));
		
	}
	void initQueryAllEmployeePanel() {
		queryAllText = new JTextArea();
		queryAllPanel = new JScrollPane(queryAllText);
		
	}
	void initModifyEmployeePanel() {
		modifyEmployeePanel = new JPanel();
		modifyEmployeePanel.setLayout(null);
		JLabel title = new JLabel("Modify Employee Module");
		JLabel queryId = new JLabel("Please enter the ID:");
		JLabel id = new JLabel("id:");
		JLabel name = new JLabel("name:");
		JLabel age = new JLabel("age:");
		JLabel gender = new JLabel("gender:");
		modifyEmployeeIdText= new JTextField(20);
		modifyEmployeeNameText= new JTextField(20);
		modifyEmployeeAgeText= new JTextField(20);
		modifyEmployeeGenderText= new JTextField(20);
		modifyEmployeeQueryIdText= new JTextField(20);
		JButton submit = new JButton("query");
		JButton modifySubmit = new JButton("modify");
		JButton modifyCancel = new JButton("cancel");
		modifyEmployeePanel.add(queryId);
		queryId.setBounds(160, 120, 150, 30);
		modifyEmployeePanel.add(modifyEmployeeQueryIdText);
		modifyEmployeeQueryIdText.setBounds(280, 120, 230, 30);
		modifyEmployeePanel.add(submit);
		submit.setBounds(520, 120, 100, 30);
		modifyEmployeePanel.add(title);
		title.setFont(new Font("����",Font.BOLD,24));
		title.setBounds(260, 50, 400, 30);
		modifyEmployeePanel.add(id);
		id.setBounds(210, 180, 50, 30);
		id.setFont(new Font("����",Font.BOLD,16));
		modifyEmployeePanel.add(modifyEmployeeIdText);
		modifyEmployeeIdText.setBounds(280, 180, 280, 30);
		modifyEmployeePanel.add(name);
		name.setBounds(210, 230, 50, 30);
		name.setFont(new Font("����",Font.BOLD,16));
		modifyEmployeePanel.add(modifyEmployeeNameText);
		modifyEmployeeNameText.setBounds(280, 230, 280, 30);
		modifyEmployeePanel.add(age);
		age.setBounds(210, 280, 50, 30);
		age.setFont(new Font("����",Font.BOLD,16));
		modifyEmployeePanel.add(modifyEmployeeAgeText);
		modifyEmployeeAgeText.setBounds(280, 280, 280, 30);
		modifyEmployeePanel.add(gender);
		gender.setBounds(210, 330, 70, 30);
		gender.setFont(new Font("����",Font.BOLD,16));
		modifyEmployeePanel.add(modifyEmployeeGenderText);
		modifyEmployeeGenderText.setBounds(280, 330, 280, 30);
		modifyEmployeePanel.add(modifySubmit);
		modifySubmit.setBounds(240, 400, 100, 30);
		modifyEmployeePanel.add(modifyCancel);
		modifyCancel.setBounds(410, 400, 100, 30);
		
		submit.addActionListener(new ModifyEmployeeAction(this));
		modifySubmit.addActionListener(new ModifyEmployeeAction(this));
		modifyCancel.addActionListener(new ModifyEmployeeAction(this));
	}
	void initInstructionPanel() {
		String s="使用说明书 \n\n"
				+ "一、Add菜单栏下的add administrator用于增加本系统的管理者\n\n"
				+"二、add employee用于添加员工信息\n\n"
				+"三、Delete菜单栏下的delete employee用于删除员工信息\n\n"
				+"四、Query菜单栏下的query one employee用于查询员工个人信息\n\n"
				+"五、query all employee用于查询群体员工信息。\r\n\n"
				+"六、Modify菜单栏下的modify employee用于修改员工信息。";

		JTextArea instruction = new JTextArea();
		instruction.append(s);
		instructionPanel = new JScrollPane(instruction);
		
	}
	void initDeveloperPanel() {
		String s="开发人员简介\n\n"
				+ "ChenLinghong：录入员工基本信息、数据结构设计 \n\n"
				+"XiaoWeiwei：录入员工基本信息、界面设计\n\n"
				+"LinChaocong：修改员工基本信息、后台实现\n\n"
				+"RenHuaifan：查询员工基本信息、界面与后台数据连接\n\n"
				+"LiuYang：删除员工基本信息、系统测试\r\n\n" ;

		JTextArea instruction = new JTextArea();
		instruction.append(s);
		developerPanel = new JScrollPane(instruction);
	}
	void initCopyDocumentPanel() {
		String s="版权声明\n\n"
				+ "本系统的版权属于JAVA三班第四组所有，与签约的客户拥有使用权。 \n\n"
				+"未经本组人员许可，所签户不得向第三方提供，也不得在媒体发表。\n\n"
				+"特此声明。\n\n";
		JTextArea instruction = new JTextArea();
		instruction.append(s);
		copyDocumentPanel = new JScrollPane(instruction);
	}
	void initWelcomePanel() {
		welcomePanel = new JPanel();
		welcomePanel.setLayout(null);
		JLabel welcome = new JLabel("Welcome to use the information management system.");
		welcome.setFont(new Font("����",Font.BOLD,22));
		welcomePanel.add(welcome);
		welcome.setBounds(100, 200, 700, 50);
	}
	void initDeleteEmployeePanel() {
		deletePanel = new JPanel();
		deletePanel.setLayout(null);
		JLabel title = new JLabel("Delete Employee Module");
		JLabel queryId = new JLabel("Please enter the ID:");
		JTextField queryIdText = new JTextField(20);
		deletePanel.add(title);
		title.setFont(new Font("����",Font.BOLD,24));
		title.setBounds(260, 50, 400, 30);
		deletePanel.add(queryId);
		queryId.setBounds(180, 120, 150, 30);
		deletePanel.add(queryIdText);
		queryIdText.setBounds(310, 120, 300, 30);
		JButton query = new JButton("query");
		JButton submit = new JButton("delete");
		deletePanel.add(query);
		query.setBounds(250, 190, 100, 30);
		deletePanel.add(submit);
		submit.setBounds(440, 190, 100, 30);
		JTextArea userInfor = new JTextArea(100,100);
		deletePanel.add(userInfor);
		userInfor.setBounds(200, 250, 400, 200);
		
		query.addActionListener(new DeleteAction(this));
		submit.addActionListener(new DeleteAction(this));
	}
	
}
